#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2011
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author: Jesus Carrero <j.o.carrero@gmail.com>
# Mountain View, CA

"""
  The marker price for a series of options with expiration dates T1, T2, ...TN
  are knows.  We also assume that for some expiration date Ti, options
  with strike values Ki1, Ki2, ...., KiMi are traded.  Given an option
  with expiration date Ti and strike Kij we let the bid and ask prices
  for the option be denoted Vij^b and Vij^a respectively.

  Problem:
    Find the local volatility function such that the predicted value of V
    falls between the corresponding div and spreads.
"""

from scipy import ones, arange, linspace

from utils.Spline import Spline
from utils.FemBases import LagrangeP1, LagrangeP2

from models.CalibBase import CalibBase
from models.NumericalOptionGBM import NumericalOptionGBM

import logging

class CalibrateDVS(CalibBase):

    def __init__(self):

        (self.m_Tmin, self.m_Tmax, self.m_Smin, self.m_Smax) = (None,
                None, None, None)

        self.logCal = logging.getLogger('CalibrateVol')

    def set_grid_spacing(self, nSegS, nSegT):
        self.m_nSegS = nSegS
        self.m_nSegT = nSegT

    def costFunction(self, sig):
        #callPrices = asarray([vanillaCallPrice(S0, K, iRate, sig, tm)
        #                 for (S0, K, tm) in zip(prices, stikes, T)])
        #putPrices = asarray([vanillaPutPrice(S0, K, iRate, sig, tm)
        #                for (S0, K, tm) in zip(prices, stikes, T)])
        optSol = r_[callPrices.flatten(), putPrices.flatten()]
        return sum((exactSol - optSol) ** 2)


    def calibrate(self):
        (self.m_Tmin, self.m_Tmax) = (min(self.m_cTi), max(self.m_cTi))
        (self.m_Smin, self.m_Smax) = (min(self.m_cSi), max(self.m_cSi))

        tSteps = linspace(self.m_Tmin, self.m_Tmax, self.m_nSegT + 1)
        sSteps = linspace(self.m_Smin, self.m_Smax, self.m_nSegS + 1)

        # generate initial guess splines.

        x = linspace(0, self.m_nSegT, self.m_nSegT, False)
        y = 0.5 * ones(x.shape)
        for i in arange(self.m_nSegS + 1):
            spLevelSi = Spline()
            spLevelSi.spline(x, y)



